<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Index extends CommonController
{
    function show($status,$message,$data=array()){
        $reuslt = array(
            'status'=>$status,
            'message'=>$message,
            'data'=>$data,
        );
        exit(json_encode($reuslt)) ;   //输出一条信息 并退出当前脚本
    }
    public function index()
    {
        $username = Session::get('service.name');
        $this->assign('name',$username);

        return $this->fetch();
    }

    public function welcome()
    {
        return $this->fetch();
    }



    //获取token
    function getaccess_token(){

        $token = Db::name('token')
            ->where(array(
                'id'=>1
            ))
            ->find();
        if($token){
            if($token['expires']+$token['update_time']<time()){
                //appid与appsecret改成你自己的
                $appid = 'wxaeae5ec2124342f4';
                $appsecret = 'ff35487505508d87adf9e680e8bd1198';
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
                $data = curl_exec($ch);
                curl_close($ch);
                $data = json_decode($data,true);

//                $da['id'] = 1;
                $da['access_token'] =$data['access_token'];
                $da['update_time'] = time();
                Db::name('token')->where('id', 1)->update($da);

                return $data['access_token'];
            }else{
                return $token['access_token'];
            }

        }

    }


}
